package com.yb.personelmanagement.domain.System;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @Auther: lzx
 * @Date: 2019/8/7 09:43
 * @Description:
 */
@Entity
@Table(name="user")
public class User implements UserDetails {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "id", columnDefinition = "bigint(20)  comment'主键自增'")
    private Long id;
    @Column(name = "username", columnDefinition = "varchar(100)  comment'用户名'")
    @NotNull(message = "用户名不能为空")
    private String userName;

    @Column(name = "password", columnDefinition = "varchar(100)  comment'用户密码'")
    @NotEmpty(message = "密码不能为空")
    private String passWord;
    @ManyToMany(fetch = FetchType.EAGER)
    private Set<Role> roles;
    
    @ManyToMany(fetch = FetchType.EAGER)
    private Set<DataRole> dataRoles;

    //@ManyToMany
    @OneToOne
    private Department department;


    @Column(name = "email", columnDefinition = "varchar(100)  comment'邮箱'")
    @Email(message = "邮箱格式不正确")
    private String email ;
    @Column(name = "lastpasswordrestdate", columnDefinition = "datetime  comment'最好密码更改时间'")
    private Date lastPasswordResetDate ;
    
    /**
     * 职务
     */
    @Column(name = "job", columnDefinition = "varchar(100)  comment'岗位'")
    private String job;
    /**
     * 移动电话
     */
    @Column(name = "mobliephone", columnDefinition = "varchar(100)  comment'移动电话'")
    private String mobilePhone;
    
    /**
     * 办公电话
     */
    @Column(name = "telephone", columnDefinition = "varchar(100)  comment'办公电话'")
    private String telePhone;
    
    /**
     * 最后登陆时间
     */
    @Column(name = "lastlogindate", columnDefinition = "datetime  comment'最后登陆时间'")
    private Date lastLoginDate;
    /**
     * 最后登出时间
     */
    @Column(name = "lastloginoutdate", columnDefinition = "datetime  comment'最后登出时间'")
    private Date lastLoginOutDate;
    
    /**
     * 临时封装 登陆后产生的token
     * 不做持久化存储
     */
    @Column(name="token")
    private String token;
    
    /**
     * 账号是否有效
     */
    @Column(name = "isenable", columnDefinition = "int(10)  comment'账号是否有效,默认1有效'")
    private Integer isenable=1;
    
    

    public String getUserName() {
        return userName;
    }



    public User() {
		super();
		// TODO Auto-generated constructor stub
	}



	public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    public Collection<Role> getRoles() {
        return roles;
    }

   

    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public Date getLastPasswordResetDate() {
        return lastPasswordResetDate;
    }

    public void setLastPasswordResetDate(Date lastPasswordResetDate) {
        this.lastPasswordResetDate = lastPasswordResetDate;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> auths=new ArrayList<>();
        Collection<Role> roles=this.getRoles();
       for (Role role:roles){
        auths.add(new SimpleGrantedAuthority(role.getRoleName()));
       }
       return auths;
    }

    @Override
    public String getPassword() {
        return this.passWord;
    }

    @Override
    public String getUsername() {
        return this.userName;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }



	public Set<DataRole> getDataRoles() {
		return dataRoles;
	}



	public void setDataRoles(Set<DataRole> dataRoles) {
		this.dataRoles = dataRoles;
	}



	public String getPassWord() {
		return passWord;
	}



	public void setUserName(String userName) {
		this.userName = userName;
	}



	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}



	public Date getLastLoginDate() {
		return lastLoginDate;
	}



	public void setLastLoginDate(Date lastLoginDate) {
		this.lastLoginDate = lastLoginDate;
	}



	public Date getLastLoginOutDate() {
		return lastLoginOutDate;
	}



	public void setLastLoginOutDate(Date lastLoginOutDate) {
		this.lastLoginOutDate = lastLoginOutDate;
	}



	public String getToken() {
		return token;
	}



	public void setToken(String token) {
		this.token = token;
	}



	

	public String getJob() {
		return job;
	}



	public void setJob(String job) {
		this.job = job;
	}



	public String getMobilePhone() {
		return mobilePhone;
	}



	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}



	public String getTelePhone() {
		return telePhone;
	}



	public void setTelePhone(String telePhone) {
		this.telePhone = telePhone;
	}



	public Integer getIsenable() {
		return isenable;
	}



	public void setIsenable(Integer isenable) {
		this.isenable = isenable;
	}



	public Department getDepartment() {
		return department;
	}



	public void setDepartment(Department department) {
		this.department = department;
	}



	public User(Long id, @NotNull(message = "用户名不能为空") String userName, @NotEmpty(message = "用户名不能为空") String passWord,
			Set<Role> roles, Set<DataRole> dataRoles, Department department, @Email(message = "邮箱格式不正确") String email,
			Date lastPasswordResetDate, String job, String mobilePhone, String telePhone, Date lastLoginDate,
			Date lastLoginOutDate, String token, Integer isenable) {
		super();
		this.id = id;
		this.userName = userName;
		this.passWord = passWord;
		this.roles = roles;
		this.dataRoles = dataRoles;
		this.department = department;
		this.email = email;
		this.lastPasswordResetDate = lastPasswordResetDate;
		this.job = job;
		this.mobilePhone = mobilePhone;
		this.telePhone = telePhone;
		this.lastLoginDate = lastLoginDate;
		this.lastLoginOutDate = lastLoginOutDate;
		this.token = token;
		this.isenable = isenable;
	}
	
}
